package cn.Liujialin.user.service.impl;

import cn.Liujialin.basic.service.impl.BeanServiceImpl;
import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.MD5Utils;
import cn.Liujialin.basic.util.StrUtils;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.user.domain.LoginInfo;
import cn.Liujialin.user.domain.User;
import cn.Liujialin.user.dto.UserDto;
import cn.Liujialin.user.mapper.LoginInfoMapper;
import cn.Liujialin.user.mapper.UserMapper;
import cn.Liujialin.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

import static cn.Liujialin.basic.stant.BeanContant.BUSINESS_TYPE_REGISTER;

@Service
public class UserServiceImpl extends BeanServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //基本的Crud的实现来自于继承BaseServiceImpl

    //如果后期有扩展的自己写

    //注册是否成功
    @Override
    public AjaxResult register(UserDto userdto) {
//        1、所有参数不能为空
        if(StringUtils.isEmpty(userdto.getPhone())|| StringUtils.isEmpty(userdto.getVerifyCode())
                || StringUtils.isEmpty(userdto.getPassword()) || StringUtils.isEmpty(userdto.getConfirmPassword())
                ){
            throw  new  BusinessException("信息不能为空");

        }
//        2、所有密码要一致
        if (!userdto.getPassword().equals(userdto.getConfirmPassword())){
            throw new BusinessException("密码要一致");
        }
//        3、验证码是否自己失效，也就是判断验证码是否为空
         //获取验证码
         Object value = redisTemplate.opsForValue().get(BUSINESS_TYPE_REGISTER+userdto.getPhone());
        if (value==null){
            throw new BusinessException("验证码失效了");
        }

//        4、验证码是否一致（你传递过来的验证码是否和我的一致）截取出字符串
        //忽略大小写   IgnoreCase   一定不要忘记toString
        String values = (String)value;
        String val = values.toString().split(":")[0];
        if (!userdto.getVerifyCode().equalsIgnoreCase(val)){
            throw new BusinessException("请输入正确验证码");
        }
//        5、手机号不能被注册（要查数据库）
        User user = userMapper.findByPhone(userdto.getPhone());
        if (user!=null){
            throw new BeanCreationException("该用户已被注册");

        }
//        6、保存数据（先保存到LoginInfo 然后再保存到user 因为user有外键而LoginInfo没有外键）
//        userDto2User 某个方法将userDto转为User对象 用User来接收
        /*User userTmp = userdto2User(userdto);
        userMapper.save(userTmp);
        LoginInfo loginInfo = User2loginInfo(userTmp);
        loginInfoMapper.save(loginInfo); 写错了的*/

        User userTmp = userDto2User(userdto);
        LoginInfo loginInfo = user2LoginInfo(userTmp);
        //先储存进登录表中
        loginInfoMapper.save(loginInfo);//一定要返回自增ID
       //返回自增id
        userTmp.setLogininfo_id(loginInfo.getId());

        //因为有外键 所以才在登录表之后保存
        userMapper.save(userTmp);

        return AjaxResult.me();
    }

    private User userDto2User(UserDto userDto) {
        User users = new User();
        users.setUsername(userDto.getPhone());
        users.setPhone(userDto.getPhone());
        users.setEmail(userDto.getPhone());
        //users.setPassword(userDto.getPassword());将密码加密之后在传入
        //获取32位盐值
        String stat = StrUtils.getComplexRandomString(32);
        //使用盐值加密密码
        String md5password = MD5Utils.encrypByMd5(userDto.getPassword()+stat);
        //在将密码传入
        users.setPassword(md5password);
        users.setSalt(stat);
        //返回对象
        return users;


    }

    //把User转换为loginInfo
    private LoginInfo  user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();

        //属性同名原则拷贝,把相同数据的对象拷贝过去
        BeanUtils.copyProperties(user,loginInfo);

        return loginInfo;
    }


}
